﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Repository;
using Estimate.Common.Estimate;
using Newtonsoft.Json;

namespace Estimate.Reports
{
    /// <summary>
    /// 标准报表数据接口
    /// </summary>
    public class StandardReport
    {
        private FullInfoReport memberReportScore;// 得分对象
        private FullInfoReport standardReportScore;// 标准评分对象
        ExecutionStatus exeStatus;//程序执行状态
        StandardReportResult Result = new StandardReportResult();
        protected string _connectionString;
        string Cities = "";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ConnectionString"></param>
        /// <param name="tableName">数据表类型</param>
        /// <param name="tid">程序执行任务号</param>
        public StandardReport(string ConnectionString, DataTableType tableName,string cities, string tid)
        {

            _connectionString = ConnectionString;

            Dictionary<string, string> CityCodes = null;

            if (cities != null && cities != "")
            {
                Cities = cities;
                CityCodes = new Dictionary<string, string>();
                CityCodes.Add(cities, cities);

                //string[] strs = cities.Split(',');
                //if (strs.Length > 0)
                //    CityCodes = new Dictionary<string, string>();
                //foreach (string city in strs)
                //    CityCodes.Add(city, city);
            }
            else
                Cities = "";

            if (tid != "")
                exeStatus = new ExecutionStatus(tid);

            ForeRepository DataProvider = null;// 获取semi的数据检索对象

             switch (tableName)
             {
                 case DataTableType.city: //城镇预报
                     DataProvider = ForeRepositoryFactory.NormalCityForeRepository(_connectionString);
                     break;
                 case DataTableType.zd://指导站预报
                     DataProvider = ForeRepositoryFactory.StSiteForeRepository(_connectionString);
                     break;
                 case DataTableType.js: //降水等值线
                     DataProvider = ForeRepositoryFactory.RainForeRepository(_connectionString);
                     break;
                 case DataTableType.zyt://国家局下
                     DataProvider = ForeRepositoryFactory.ZytCityForeRepository(_connectionString);
                     break;
                 default:
                     break;
             }

             memberReportScore = new FullInfoReport(_connectionString, DataProvider, CityCodes);
             memberReportScore.CulculateAllResult = true;
             memberReportScore.CulculateDeviationResult = false;
             memberReportScore.ExeStatus = exeStatus;

             standardReportScore = new FullInfoReport(_connectionString, ForeRepositoryFactory.ZytCityForeRepository(_connectionString), CityCodes);
             standardReportScore.CulculateAllResult = true;
             standardReportScore.CulculateDeviationResult = false;
             standardReportScore.ExeStatus = exeStatus;
        }

        public void PrepareData(DateTime beginDate, DateTime endDate, ForeSemiType semitype, ForeSpanType[] waterSpans, ForeSpanType[] tempSpans, ForeSendpointType[] sendpoints)
        {
            try
            {
                if (exeStatus != null)
                {
                    exeStatus.TotalSteps = memberReportScore.GetExeStatusTotalSteps(beginDate, endDate, semitype, waterSpans, tempSpans) + standardReportScore.GetExeStatusTotalSteps(beginDate, endDate, semitype, waterSpans, tempSpans);
                    exeStatus.SetMessage("memberReportScore", "计算标准得分");
                }

                memberReportScore.PrepareData(beginDate, endDate, semitype, waterSpans, tempSpans, sendpoints);

                if (exeStatus != null)
                {
                    exeStatus.DeleteMessage("memberReportScore");
                    exeStatus.SetMessage("standardReportScore", "计算技巧值评分标准");
                }


                standardReportScore.PrepareData(beginDate, endDate, semitype, waterSpans, tempSpans, sendpoints);

                if (exeStatus != null) exeStatus.DeleteMessage("standardReportScore");
            }
            catch (Exception ex)
            {
                Result.success = false.ToString();
                Result.msg = ex.Message;
                if (exeStatus != null)
                {
                    exeStatus.DeleteMessage("memberReportScore");
                    exeStatus.DeleteMessage("standardReportScore");
                    exeStatus.SetMessage("Error", "程序执行出错");
                }
            }
        }

        /// <summary>
        /// 获取核查表数据字符串
        /// </summary>
        /// <returns></returns>
        public string GetReportDataString()
        {
            if (exeStatus != null) exeStatus.SetMessage("GetReportDataString", "正在获得计算结果");

            StandardReportResult result = GetReportData();

            if (exeStatus != null)
            {
                exeStatus.DeleteMessage("GetReportDataString");
                exeStatus.SetMessage(true, "ExecutionEnd", "程序执行完毕");

            }

            return JsonConvert.SerializeObject(result);

        }

        public StandardReportResult GetReportData()
        {
            Result.result.Add("data", memberReportScore.ToResult(standardReportScore, Cities));
            return Result;
        }

        public void Clear()
        {
            memberReportScore.Clear();
            standardReportScore.Clear();
            Result.success = true.ToString();
            Result.msg = "";
            Result.result = new Dictionary<string, object>();
        }
    }

    /// <summary>
    /// 获取标准表数据
    /// </summary>
    public class StandardReportResult
    {
        public string success = true.ToString();
        public string msg = "";
        public Dictionary<string, object> result = new Dictionary<string, object>();

        public StandardReportResult()
        {
           
        }
    }
    
}
